package com.glela.goods.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.glela.common.util.DateUtil;
import com.glela.common.util.NumberUtil;
import com.glela.goods.mapper.GoodsMapper;
import com.glela.goods.mapper.GoodsNavigateMapper;
import com.glela.goods.mapper.InventoryMapper;
import com.glela.goods.mapper.ProductMapper;
import com.glela.goods.model.Goods;
import com.glela.goods.model.GoodsNavigate;
import com.glela.goods.model.GoodsPrice;
import com.glela.goods.model.Inventory;
import com.glela.goods.model.Product;
import com.glela.goods.model.SpecificationDetailVo;
import com.glela.goods.model.response.ProductResponse;
import com.glela.goods.model.vo.ProductVo;
import com.glela.goods.model.vo.inventory.DynamicInventoryVo;
import com.glela.goods.service.GoodsService;
import com.glela.goods.service.InventoryService;
import com.glela.goods.service.ProductService;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.mapper.UserMapper;
import com.glela.platform.model.User;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;

@Service("productService")
@Scope("singleton")
public class ProductServiceImpl implements ProductService {

	@Autowired
	private ProductMapper productMapper;
	@Autowired
	private InventoryMapper inventoryMapper;
	@Autowired
	private GoodsMapper goodsMapper;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private InventoryService inventoryService;
	@Autowired
	private GoodsNavigateMapper goodsNavigateMapper;

	@Override
	public ResponseEntity<?> getProductByGoodIdAndSpecInfo(long goodId, String specInfo, int shareUserRoleId,
			int userRoleId) {
		Goods goods = goodsMapper.selectGoodsByIdTwo(goodId);
		if (goods == null) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20401), HttpStatus.OK);
		}
		Product product = productMapper.selectProductByGoodIdAndSpecInfo(goodId, specInfo);
		if (product == null) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20402), HttpStatus.OK);
		}
		Inventory inventory = inventoryMapper.selectInventoryByProductId(product.getId());
		if (inventory == null) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20409), HttpStatus.OK);
		}
		ProductVo vo = new ProductVo(product);
		vo.setSettlementPrice(inventory.getSettlementPrice());// 结算成本价(单位分)
		if (shareUserRoleId > 1 || userRoleId > 1) {
			vo.setSalePrice(inventory.getSalePrice() * (100 - goods.getTalentLimit()) / 100);// 实际销售价(单位分)
		} else {
			vo.setSalePrice(inventory.getSalePrice());// 实际销售价(单位分)
		}
		vo.setInventoryNum(inventory.getInventoryNum());
		vo.setInventoryId(inventory.getId());
		vo.setStoreId(inventory.getStoreId());
		List<SpecificationDetailVo> specDescList = JSON.parseArray(product.getSpecDesc(), SpecificationDetailVo.class);
		vo.setSpecDescList(specDescList);
		StringBuffer specName = new StringBuffer();
		for (SpecificationDetailVo spec : specDescList) {
			specName.append(spec.getDetailName()).append("  ");
		}
		vo.setSpecName(specName.toString());
		return new ResponseEntity<>(new ResponseResultModel<ProductVo>(vo), HttpStatus.OK);
	}
	
	@Override
	public ResponseEntity<?> getProductInfoByGoodIdAndSpecInfo(long goodId,String specInfo,Integer storeUserId) {
		Goods goods = goodsMapper.selectGoodsByIdTwo(goodId);
		if (goods == null) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20401), HttpStatus.OK);
		}
		Product product = productMapper.selectProductInfoByGoodIdAndSpecInfo(goodId, specInfo);
		if (product == null) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C40011), HttpStatus.OK);
		}
		// 查找规格图片  
		String specificationImagesPath = "";
		if(specInfo.indexOf("|") != -1){
			int specificationDetailId = Integer.parseInt(specInfo.split("\\|")[0]);
			specificationImagesPath = this.goodsMapper.getSpecificationImagesPath(goodId,specificationDetailId);
		}
		if("".equals(specificationImagesPath)){
			// 商品介绍图
			List<GoodsNavigate> goodsNavigateList = this.goodsNavigateMapper.selectGoodsNavigateByGoodId(goodId);
			if(goodsNavigateList != null){
				specificationImagesPath = goodsNavigateList.get(0).getNavigateImage();
			}
		}
		ProductResponse productResponse = new ProductResponse();
		productResponse.setGoodId(goodId);
		productResponse.setProductId(product.getId());
		productResponse.setSpecificationImagesPath(specificationImagesPath);
		// 查询库存
		DynamicInventoryVo dynamicInventoryVo = this.inventoryService.findDynaByProductId(product.getId());
		if(dynamicInventoryVo == null || product.getStatus() == 0){
			productResponse.setInventoryId(0);
			productResponse.setInventoryNum(0);
		}else{
			if(dynamicInventoryVo.getInventoryId()==null){
				productResponse.setInventoryId(0);
				productResponse.setInventoryNum(0);
			} else {
				productResponse.setInventoryId(dynamicInventoryVo.getInventoryId());
				productResponse.setInventoryNum(dynamicInventoryVo.getInventoryNumAvl());
			}
		}
		// 是否下架
		String isUnderCarriage = "no";
		if(product.getStatus() == 0){
			isUnderCarriage = "yes";
		}
		productResponse.setIsUnderCarriage(isUnderCarriage);
		//价格信息
		User user = this.userMapper.selectByPrimaryKey(Long.valueOf(storeUserId));
		GoodsPrice goodsPrice = this.goodsService.getGoodsViewPriceForProduct(user, product.getId());
		productResponse.setGoodsPrice(goodsPrice);
		//分时抢购信息
		String nowTime = DateUtil.currentStringDate();
		Map<String,Object> ltsMap = this.goodsMapper.selectLimitTimeSale(goods.getId(),product.getId(),nowTime);
		if(ltsMap == null){
			ltsMap = new HashMap<String, Object>();
			ltsMap.put("productSalePrice", -999);
			ltsMap.put("userSkuCount", -999);
		}else {
			int showCount = Integer.parseInt(ltsMap.get("showCount").toString());
			if (showCount < productResponse.getInventoryNum()) {
				productResponse.setInventoryNum(showCount);
			}
			ltsMap.remove("showCount");
			ltsMap.put("productSalePrice", NumberUtil.getMoney(Long.parseLong(ltsMap.get("productSalePrice").toString()))+"");
		}
		productResponse.setLtsMap(ltsMap);
		return new ResponseEntity<>(new ResponseResultModel<ProductResponse>(productResponse), HttpStatus.OK);
	}
	
}
